package cn.comtom.ewbs.signature.service.impl;
import cn.comtom.ewbs.signature.entity.SignApply;
import cn.comtom.ewbs.signature.mapper.SignApplyMapper;
import cn.comtom.ewbs.signature.service.ISignApplyService;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.io.ByteSink;
import com.google.common.io.Files;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.comtom.ewbs.signature.common.enums.SysConstant;
import cn.comtom.ewbs.signature.common.vo.request.PageRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuhy
 * @since 2022-11-08 18:12:05
 */
@Service
@Slf4j
public class SignApplyServiceImpl extends ServiceImpl<SignApplyMapper, SignApply> implements ISignApplyService {
	
    @Autowired
    private Sjj1507ServiceImpl signatureService;
    
	@Override
	public IPage<SignApply> selectPage(SignApply signApply, PageRequest pageReq) {
		Page<SignApply> page = new Page<SignApply>(pageReq.getPage() , pageReq.getLimit() , true);
        LambdaQueryWrapper<SignApply> wrapper = Wrappers.lambdaQuery();
        wrapper.setEntity(signApply);
        return this.baseMapper.selectPage(page, wrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean signApply(String id, String account) throws IOException {
	    SignApply sa = this.getById(id);
	    sa.setApplyTime(LocalDateTime.now());
	    sa.setApplytor(account);
	    if(sa == null || sa.getApplyFile() == null) {
	    	return false;
	    }
	    FileUtil.mkdir(SysConstant.SYS_TMP_DIR);
	    String tmpFilePath = SysConstant.SYS_TMP_DIR.concat(IdUtil.fastSimpleUUID()).concat(".txt");
	    File file = new File(tmpFilePath);
	    ByteSink  myPic = Files.asByteSink(file);
    	myPic.write(sa.getApplyFile());
	    List<String> lines = FileUtil.readUtf8Lines(tmpFilePath);
		String trustStr = signatureService.trustedCertSnchainApply(lines);
		if(StrUtil.isNotBlank(trustStr)) {
			sa.setResultFile(trustStr.getBytes("utf-8"));
			sa.setResultFileName(file.getName());
			sa.setApplyStatus(1);
			sa.setApplyResult("申请成功");
		}else {
			sa.setApplyStatus(2);
			sa.setApplyResult("申请失败");
		}
		this.saveOrUpdate(sa);
		FileUtil.del(tmpFilePath);
		if(StrUtil.isNotBlank(trustStr)) {
			return true;
		}
		return false;
	}
}
